<?php

namespace App\Console\Commands;

use App\Lib\Publisher;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\RoomUser;
use App\Http\Model\Room;

class AnnualCeremonyStay extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'roomStayThirty';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新硬核CBD房间房间停留三分钟任务状态';

    protected $room;
    protected $roomUser;
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->room = new Room();
        $this->roomUser = new RoomUser();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        //获取当前房间及每个房间中的用户
        $roomIds = $this->room->getOpenedRoomIdArr();
        foreach($roomIds as $roomId) {
            $key = 'CACHE:ROOM:STAY:'.date('Ymd').':'.$roomId;
            $nowUserIds = $this->roomUser->get_usersids($roomId) ?? [];
            $lastUserIds = Redis::HKEYS($key);
            //删掉不在房间的用户缓存
            $diffUserIds = array_diff($lastUserIds ,$nowUserIds);
            foreach ($diffUserIds as $k=>$diffUserId){
                Redis::HDEL($key,$diffUserId);
            }
            foreach ($nowUserIds as $nowUserId){
                $stay_finish_key = 'CACHE:USER:STAY:'.date('Ymd').':'.$nowUserId;
                $time = Redis::hget($key,$nowUserId);
                if(empty($time)){
                    Redis::HSET($key,$nowUserId ,5);
                }else{
                    Redis::HINCRBY($key,$nowUserId ,5);
                    if($time == 25){
                        $is_finish = Redis::HGET($stay_finish_key ,$nowUserId);
                        if(empty($is_finish)){
                            Redis::HSET($stay_finish_key ,$nowUserId ,1);
                            $publishObject = Publisher::getInstance();
                            $publishObject->requestData = [
                                'queue_route' => 'global.room.stay',
                                'data' => [
                                    'user_id' => $nowUserId,
                                    'num' =>30 ,
                                    'time' => time()
                                ],
                            ];
                            $publishObject->publisher();
                        }
                    }
                }
                //设置已完成用户 24小时到期
                if(Redis::TTL($stay_finish_key) == -1){
                    Redis::EXPIRE($stay_finish_key, 1 * 24 * 3600);
                }
            }
            //设置房间用户停留缓存 24小时到期
            if(Redis::TTL($key) == -1){
                Redis::EXPIRE($key, 1 * 24 * 3600);
            }
        }
    }
}
